package org.petstore.web.servlet;

import org.petstore.domain.Item;
import org.petstore.service.CatalogService;
import org.petstore.service.impl.CatalogServiceImpl;
import org.petstore.web.servlet.util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ViewItemServlet extends HttpServlet {
    private static final String VIEW_ITEM = "/WEB-INF/jsp/catalog/Item.jsp";

    private final CatalogService catalogService = MyUtil.getApplicationContext().getBean("catalogService", CatalogService.class);

    private String productId;
    private String itemId;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //productId = request.getParameter("productId");
        itemId = request.getParameter("itemId");
        // Product product=catalogService.getProductByProductId(productId);

        //todo
        Item item = catalogService.getItem(itemId);

        HttpSession session = request.getSession();

        session.setAttribute("item", item);
        //session.setAttribute("product",product);

        request.getRequestDispatcher(VIEW_ITEM).forward(request, response);
    }
}
